package org.easy.rocketmq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * <pre>
 *     如果报错A component required a bean of type 'org.apache.rocketmq.spring.core.RocketMQTemplate' that could not be found.时
 *     补充以下配置文件到application.yml
 * rocketmq:
 *   name-server: 192.168.110.200:9876
 *   producer:
 *       group: default
 * </pre>
 */
@Configuration
@EnableConfigurationProperties
@Slf4j
@ConditionalOnProperty(prefix = "rocketmq",
        value = {"name-server", "producer.group"},
        matchIfMissing = true
)
public class RocketmqAutoConfiguration {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Bean
    @ConditionalOnMissingBean(CommonRocketMqSender.class)
    public CommonRocketMqSender rocketMqCommonMsgProducer() {
        return new CommonRocketMqSender(rocketMQTemplate);
    }
}
